package com.J1ay.carsys.service.Impl;

import com.J1ay.carsys.dao.BowerMapper;
import com.J1ay.carsys.dao.CarMapper;
import com.J1ay.carsys.dao.EmpMapper;
import com.J1ay.carsys.dto.LoginInput;
import com.J1ay.carsys.dto.LoginOutput;
import com.J1ay.carsys.pojo.Bower;
import com.J1ay.carsys.pojo.Car;
import com.J1ay.carsys.pojo.Result;
import com.J1ay.carsys.service.EmpService;
import com.J1ay.carsys.utils.Common;
import com.J1ay.carsys.utils.MybatisUtils;
import org.apache.ibatis.exceptions.PersistenceException;

import java.util.List;

/**
 * @Author J1ay
 * @Date 2021/6/3 20:48
 * @Description
 */

public class EmpServiceImpl implements EmpService {

    @Override
    public Result login(LoginInput loginInput){
//        LoginOutput loginOutput = MybatisUtils.getSqlSession().getMapper(EmpMapper.class).EmpLoginByPwd(loginInput);
        LoginOutput loginOutput = MybatisUtils.getSqlSession().getMapper(EmpMapper.class).EmpLoginByPwd(Common.getMapperFactory().getMapperFacade().map(loginInput,LoginInput.class));
        if (null != loginOutput) {
            return Common.resultReturn(loginOutput);
        } else {
            return Common.resultReturn(null, 400,"用户名与密码不匹配!");
        }
    }

    @Override
    public Result addCar(Car car) {
        try {
            MybatisUtils.getSqlSession().getMapper(CarMapper.class).addCar(Common.getMapperFactory().getMapperFacade().map(car, Car.class));
            return Common.resultReturn(null);
        } catch (PersistenceException e) {
            return Common.resultReturn(null, 400, "数据错误");
        } catch (Exception e) {
            e.printStackTrace();
            return Common.resultReturn(null, 500, "内部错误");
        }
    }

    @Override
    public Result deleteCar(int id, String num){
        try {
            MybatisUtils.getSqlSession().getMapper(CarMapper.class).deleteCar(id, num);
            return Common.resultReturn(null);
        } catch (PersistenceException e) {
            return Common.resultReturn(null, 400, "数据错误");
        } catch (Exception e) {
            e.printStackTrace();
            return Common.resultReturn(null, 500, "内部错误");
        }
    }

    @Override
    public Result updateCar(Car car){
        try {
            MybatisUtils.getSqlSession().getMapper(CarMapper.class).updateCar(Common.getMapperFactory().getMapperFacade().map(car, Car.class));
            return Common.resultReturn(null);
        } catch (PersistenceException e) {
            return Common.resultReturn(null, 400, "数据错误");
        } catch (Exception e) {
            e.printStackTrace();
            return Common.resultReturn(null, 500, "内部错误");
        }
    }

    @Override
    public Result getCarList() {
        return Common.resultReturn(MybatisUtils.getSqlSession().getMapper(CarMapper.class).getCarList());
    }

    @Override
    public Result getCarById(int id, String num) {
        return Common.resultReturn(MybatisUtils.getSqlSession().getMapper(CarMapper.class).getCarById(id, num));
    }

    @Override
    public Result getBowerList() {
        return Common.resultReturn(MybatisUtils.getSqlSession().getMapper(BowerMapper.class).getBowerList());
    }

    @Override
    public Result checkBowerList() {
        return Common.resultReturn(MybatisUtils.getSqlSession().getMapper(BowerMapper.class).checkBowerList());
    }
    @Override
    public Result getBowerByPhone(String phone) {
        Bower bower = MybatisUtils.getSqlSession().getMapper(BowerMapper.class).getBowerByPhone(phone);
        if (null != bower) {
            return Common.resultReturn(bower);
        }
        else {
            return Common.resultReturn(null,404,"找不到该用户(可能还在审核中)");
        }
    }

    @Override
    public Result addBower(Bower bower) {
        try {
            MybatisUtils.getSqlSession().getMapper(BowerMapper.class).addBower(Common.getMapperFactory().getMapperFacade().map(bower, Bower.class));
            return Common.resultReturn(null);
        } catch (PersistenceException e) {
            return Common.resultReturn(null, 400, "数据错误");
        } catch (Exception e) {
            e.printStackTrace();
            return Common.resultReturn(null, 500, "内部错误");
        }
    }

    @Override
    public Result updateBower(Bower bower) {
        try {
            MybatisUtils.getSqlSession().getMapper(BowerMapper.class).updateBower(Common.getMapperFactory().getMapperFacade().map(bower, Bower.class));
            return Common.resultReturn(null);
        } catch (PersistenceException e) {
            return Common.resultReturn(null, 400, "数据错误");
        } catch (Exception e) {
            e.printStackTrace();
            return Common.resultReturn(null, 500, "内部错误");
        }
    }

    @Override
    public Result checkBower(Bower bower) {
        try {
            MybatisUtils.getSqlSession().getMapper(BowerMapper.class).checkBower(Common.getMapperFactory().getMapperFacade().map(bower, Bower.class));
            return Common.resultReturn(null);
        } catch (PersistenceException e) {
            return Common.resultReturn(null, 400, "数据错误");
        } catch (Exception e) {
            e.printStackTrace();
            return Common.resultReturn(null, 500, "内部错误");
        }
    }

    @Override
    public Result deleteBower(String UId, String phone) {
        try {
            MybatisUtils.getSqlSession().getMapper(BowerMapper.class).deleteBower(UId, phone);
            return Common.resultReturn(null);
        } catch (PersistenceException e) {
            return Common.resultReturn(null, 400, "数据错误");
        } catch (Exception e) {
            e.printStackTrace();
            return Common.resultReturn(null, 500, "内部错误");
        }
    }
}
